/*
 * @Date: 2019-10-13 14:05:24
 * @Author: jiangsenlin
 * @Description: 简易的一个日期帮助类
 */


const timestampToString = (date, fromat = 'YYYY-MM-DD') => {
    const dateObj = getDateFun(date);
    const dateArray = [dateObj.year, dateObj.month, dateObj.day];
    const timeArray = [dateObj.hour, dateObj.minutes, dateObj.seconds];
    let renderTimeString = '';
    const fromatLower = fromat.toLowerCase();
    switch (fromatLower) {
        case 'yyyy-mm-dd':
            renderTimeString = dateArray.join('-');
            break;
        case 'hh:mm:ss':
            renderTimeString = timeArray.join(':');
            break;
        case 'yyyy-mm-dd hh:mm:ss':
            renderTimeString = `${dateArray.join('-')} ${timeArray.join(':')}`
            break;
        case '年月日':
            renderTimeString = `${dateObj.year}年${dateObj.month}月${dateObj.day}日`
            break;
        case '月日':
            renderTimeString = `${dateObj.month}月${dateObj.day}日`
            break;
        default:
            renderTimeString = `${dateArray.join('-')} ${timeArray.join(':')}`
    }

    return renderTimeString;

}

const getDateFun = (date) => {
    const timestamp = new Date(date);
    const year = timestamp.getFullYear();
    const month = timestamp.getMonth() + 1;
    const day = timestamp.getDate();
    const hour = timestamp.getHours();
    const minutes = timestamp.getMinutes();
    const seconds = timestamp.getSeconds();
    return {
        year,
        month,
        day,
        hour,
        minutes,
        seconds
    }
}


// 分钟格式话    1天2小时48分

function minutesFormage(minutes) {
    const unit = 24 * 60;
    let day, hour, min;
    day = Math.floor(Number(minutes) / unit);
    hour = Math.floor((Number(minutes) - (day * unit)) / 60);
    min = (Number(minutes) - (day * unit) - (hour * 60));

    return `${day}天${hour}小时${min}分`;

}


/**
 * 将指定秒数转换为时分秒字符传
 * @param {number} seconds 需要格式化的秒数
 * @param {string} hourText 小时描述字符
 * @param {string} minuteText 分钟描述字符
 * @param {string} secondText 秒钟描述字符
 * @returns {string} 转换结果
 */
 const formatSeconds = (seconds, hourText = '时', minuteText = '分', secondText = '秒') => {
    let minutes = 0;
    let hours = 0;
    let result = '';
  
    if (seconds > 60) {
      minutes = Math.floor(seconds / 60);
      seconds = Math.floor(seconds % 60);
      if (minutes > 60) {
        hours = Math.floor(minutes / 60);
        minutes = Math.floor(minutes % 60);
      }
    }
  
    if (seconds < 10 && seconds > 0) {
      result = `0${seconds}${secondText}`;
    } else {
      result = `${seconds}${secondText}`;
    }
  
    if (minutes < 10 && minutes > 0) {
      result = `0${minutes}${minuteText}${result}`;
    } else {
      result = `${minutes}${minuteText}${result}`;
    }
  
    if (hours > 0) {
      result = `${hours}${hourText}${result}`;
    }
  
    return result;
  };


export default {
    timestampToString,
    minutesFormage,
    formatSeconds
}





